package sketch.seg.cd.exp;

import java.io.File;

import sketch.doc.Sketch;
import sketch.ink.SampledPoint;
import sketch.ink.Stroke;
import sketch.seg.cd.CDStaticKH;

public class CDExpStatistic {

	private double rho[];

	public CDExpStatistic(double[] rs) {
		rho = rs;
	}

	public int getN() {
		return rho.length;
	}

	public double getMean() {
		double sum = 0;
		for (double d : rho) {
			sum += d;
		}
		return sum / getN();
	}

	public double getD() {
		double mean = getMean();
		double sum = 0;
		for (double d : rho) {
			double dis = d - mean;
			sum += dis * dis;
		}
		return sum / getN();
	}

	public static void main(String[] args) {
		String dir = "C:\\Workspace\\Exp\\segmentation\\sample\\zhengwt\\";
		String file = "zhengwt.seg.8";
		Sketch sketch = new Sketch();
		sketch.readFromFile(new File(dir + file));
		Stroke s = sketch.getStrokes().get(0);

		SampledPoint[] ps = new SampledPoint[s.getPoints().size()];
//		System.out.println(ps.length);
		s.getPoints().toArray(ps);
		double[] rs = new double[ps.length];

		for (int i = 0; i < rs.length; i++) {
			rs[i] = CDStaticKH.relativeRho(ps, i, 3);
		}

		CDExpStatistic exp = new CDExpStatistic(rs);
		System.out.println("mean = " + exp.getMean());
		System.out.println("d    = " + exp.getD());

		double h = exp.getMean() + 2 * exp.getD();
		int count = 0;
		boolean going = false;
		for (double d : rs) {
			if (d > h && !going) {
				count ++;
				going = true;
				System.out.println("big value of rho: " + d);
			}
			if (d < h && going) {
				going = false;
			}
		}
		System.out.println("value of h : " + h);
		System.out.println("count of value exccess h : " + count);
	}

}
